package com.example.dzcfinal;

import android.app.FragmentTransaction;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;

import androidx.fragment.app.FragmentActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.dzcfinal.SQLite.SQLite;
import com.example.dzcfinal.fragment.AddFragment;
import com.example.dzcfinal.fragment.HomeFragment;
import com.example.dzcfinal.fragment.ProfileFragment;

import java.util.List;

public class MainActivity extends FragmentActivity implements View.OnClickListener {
    //定义变量
    public Integer userId;

    private LinearLayout homeBox;
    private LinearLayout profileBox;

    private android.app.Fragment homeFt;
    private android.app.Fragment addFt;
    private android.app.Fragment profileFt;

    public Integer index=0;

    public Integer getUserId() {
        return userId;
    }

    public void setUserId(Integer userId) {
        this.userId = userId;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initHandle();
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        if (bundle!=null){
            userId = bundle.getInt("userId");
            index = bundle.getInt("index");
        }else {
            userId = -1;
        }

        initFragment(index);

    }
    //基本操作
    protected void initHandle(){
        homeBox = findViewById(R.id.homeBox);
        profileBox = findViewById(R.id.profileBox);

        homeBox.setOnClickListener(this);
        profileBox.setOnClickListener(this);

    }

    //初始化ft
    protected void  initFragment(int index){
        FragmentTransaction ft = getFragmentManager().beginTransaction();
        switch (index){
            case 0:
                if (homeFt ==null){
                    homeFt = new HomeFragment();
                }
                ft.replace(R.id.main_content,homeFt);
                break;
            case 2:
                if(profileFt==null){
                    profileFt = new ProfileFragment();
                }
                ft.replace(R.id.main_content,profileFt);
                break;
            default:
                break;
        }
        ft.commit();
    }


    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.homeBox:
                initFragment(0);
                break;
            case R.id.profileBox:
                initFragment(2);
                break;
        }
    }

    public static boolean isIntentAvailable(Context context, Intent intent) {
        final PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
                0);//PackageManager.GET_ACTIVITIES
        return list.size() > 0;
    }
}